package com.imageim.yimirror.common.ui.analysis;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.analysisPhoto.AnalysisPhotoResultBean;
import com.imageim.yimirror.bean.analysisPhoto.PhotoNoBean;
import com.imageim.yimirror.common.ui.DropDownView;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.imageim.yimirror.common.ui.drawbody.BodyLineView;
import com.imageim.yimirror.common.util.SoundUtil;
import com.imageim.yimirror.service.statistic.StatisticSDK;
import com.imageim.yimirror.service.statistic.StatisticUtil;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

/**
 * Created by Bruce.Zhou on 2018/6/27 17:15.
 * Email: 907160968@qq.com
 */

public class AnalysisPhotoView2 extends RelativeLayout implements View.OnClickListener {

    private AnalysisPhotoResultBean analysisBean;
    private String pageId;
    private ImageView ivAvatar;
    private ImageView ivAvatarBg;
    //    private PhotoNoBean photoBean;

    public AnalysisPhotoView2(Context context) {
        super(context);
        init(context);
    }

    public AnalysisPhotoView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AnalysisPhotoView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private ImageView ivPhoto;
    private GenderTipView genderTipView;
    private BodyLineView bodyLineView;
    private ViewGroup panelParamDetail;
    private ImageView ivArrow1;
    private TextView tvParam1;
    private TextView tvParam2;
    private TextView tvParam3;
    private TextView tvParam4;
    private TextView tvDesc1;
    private TextView tvDesc2;
    private TextView tvDesc3;
    private TextView tvDesc4;
    private View panelBodyLine;
    private DropDownView dropDownView;
    private View dropHeadView;
    private View dropExpandedView;

    private void init(Context context) {
        View root = View.inflate(context, R.layout.layout_analysis_photo_view2, this);
        // init drop down view
        dropDownView = root.findViewById(R.id.drop_down_view);
        dropHeadView = LayoutInflater.from(context).inflate(R.layout.layout_body_param_title, null, false);
        dropExpandedView = LayoutInflater.from(context).inflate(R.layout.layout_body_param_detail, null, false);
        dropDownView.setHeaderView(dropHeadView);
        dropDownView.setExpandedView(dropExpandedView);
        dropHeadView.setOnClickListener(v -> {
            if (dropDownView.isExpanded()) {
                dropDownView.collapseDropDown();
                ivArrow1.setImageResource(R.drawable.ic_arrow_white_up);
            } else {
                dropDownView.expandDropDown();
                ivArrow1.setImageResource(R.drawable.ic_arrow_white_down);
            }
        });
        bodyLineView = root.findViewById(R.id.body_line_view);
        ivPhoto = root.findViewById(R.id.iv_photo);
        genderTipView = root.findViewById(R.id.panel_gender_tip);
        ivAvatar = root.findViewById(R.id.iv_avatar);
        ivAvatarBg = root.findViewById(R.id.iv_avatar_bg);
        // param
        panelParamDetail = dropExpandedView.findViewById(R.id.panel_param_detail);
        panelParamDetail.setVisibility(View.GONE);
        ivArrow1 = dropHeadView.findViewById(R.id.iv_arrow1);
        tvParam1 = dropExpandedView.findViewById(R.id.tv_param1);
        tvParam2 = dropExpandedView.findViewById(R.id.tv_param2);
        tvParam3 = dropExpandedView.findViewById(R.id.tv_param3);
        tvParam4 = dropExpandedView.findViewById(R.id.tv_param4);
        tvDesc1 = dropExpandedView.findViewById(R.id.tv_desc1);
        tvDesc2 = dropExpandedView.findViewById(R.id.tv_desc2);
        tvDesc3 = dropExpandedView.findViewById(R.id.tv_desc3);
        tvDesc4 = dropExpandedView.findViewById(R.id.tv_desc4);
        panelBodyLine = findViewById(R.id.panel_body_line);
        panelBodyLine.setVisibility(View.GONE);
    }

    public void showPanelBodyLine(boolean isPanelParamVisiable) {
        if (!isPanelParamVisiable) {
            panelParamDetail.setVisibility(View.GONE);
        } else {
            postDelayed(() -> {
                dropDownView.expandDropDown();
            }, 1100);
        }
        panelBodyLine.setVisibility(View.VISIBLE);
        ObjectAnimator moveIn = ObjectAnimator.ofFloat(panelBodyLine, "translationX", 440, 0);
        ObjectAnimator fadeIn = ObjectAnimator.ofFloat(panelBodyLine, "alpha", 0, 255);
        fadeIn.setDuration(1000);
        moveIn.setDuration(950);
        moveIn.setInterpolator(new OvershootInterpolator(1.1f));
        AnimatorSet anim = new AnimatorSet();
        anim.playTogether(moveIn, fadeIn);
        anim.start();
    }

    private boolean isPanelParamShow = false;

    private void showPanelParams(boolean show) {
        if (show) {
            panelParamDetail.setVisibility(View.VISIBLE);
            Animation dropDownAnim = AnimationUtils.loadAnimation(getContext(), R.anim.drop_down);
            dropDownAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    isPanelParamShow = true;
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            panelParamDetail.clearAnimation();
            panelParamDetail.startAnimation(dropDownAnim);
        } else {
            panelParamDetail.setVisibility(View.VISIBLE);
            Animation closeUpAnim = AnimationUtils.loadAnimation(getContext(), R.anim.close_up);
            closeUpAnim.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    isPanelParamShow = false;
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    panelParamDetail.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            panelParamDetail.clearAnimation();
            panelParamDetail.startAnimation(closeUpAnim);
        }
    }


    public void show() {
        setVisibility(View.VISIBLE);
        bodyLineView.setCanDraw(true);
        //        btnCircleClose.start(180);
        pageId = StatisticUtil.getPageId(getClass().getName());
        StatisticSDK.startPage(pageId, 1);
        SoundUtil.playAsyc(getContext(), R.raw.welcome_to_imageim, false);
    }


    public void setPhoto(String photoPath) {
        GlideUtils.display(ivPhoto, photoPath);
    }

    public void setPhoto(Bitmap bitmap) {
        ivPhoto.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                StatisticSDK.onEvent(6);
                ((Activity) getContext()).finish();
                break;
            case R.id.panel_body_line_params:
                SoundUtil.playAsyc(getContext(), R.raw.ana_result_btn, false);
                if (panelParamDetail.getVisibility() == View.VISIBLE) {
                    if (!isPanelParamShow) {
                        return;
                    }
                    showPanelParams(false);
                    ivArrow1.setImageResource(R.drawable.ic_arrow_white_up);
                } else {
                    if (isPanelParamShow) {
                        return;
                    }
                    showPanelParams(true);
                    ivArrow1.setImageResource(R.drawable.ic_arrow_white_down);
                }
                break;
            default:
        }
        StatisticSDK.endPage(pageId, 1);
    }


    public void hideBodyLine() {
        bodyLineView.setCanDraw(false);
//        bodyLineView.setVisibility(View.INVISIBLE);
    }

    public void showBodyLine() {
        bodyLineView.setCanDraw(true);
//        bodyLineView.setVisibility(View.VISIBLE);
    }

    private boolean hasPlayAnaResultSound = false;

    public void setBodyData(PhotoNoBean photoBean) {
        if (photoBean != null) {
            bodyLineView.initData(photoBean.getPoints());
            PhotoNoBean.BodyLineParam params = photoBean.getPercentages();
            if (params != null) {
                tvParam1.setText(String.format("%.3f", params.getPercentageOne()));
                tvParam2.setText(String.format("%.3f", params.getPercentageTwo()));
                tvParam3.setText(String.format("%.3f", params.getPercentageThree()));
                tvParam4.setText(String.format("%.3f", params.getPercentageFour()));
                tvDesc1.setText(params.getPercentageOneDesc());
                tvDesc2.setText(params.getPercentageTwoDesc());
                tvDesc3.setText(params.getPercentageThreeDesc());
                tvDesc4.setText(params.getPercentageFourDesc());
                showPanelBodyLine(true);
                if (!hasPlayAnaResultSound) {
                    hasPlayAnaResultSound = true;
                    SoundUtil.playAsyc(getContext(), R.raw.ana_result, false);
                }
            }
        } else {
            showPanelBodyLine(false);
        }
    }

    public void destory() {
        removeAllViews();
        genderTipView.destory();
        genderTipView = null;
        stopBodyLineView();
    }

    public void stopBodyLineView() {
        if (bodyLineView != null) {
            bodyLineView.stop();
        }
    }
}